package com.usian.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.model.article.pojos.ApAuthor;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.media.pojos.WmUser;
import com.usian.model.user.dtos.AuthDto;
import com.usian.model.user.pojos.ApUser;
import com.usian.model.user.pojos.ApUserRealname;
import com.usian.user.feign.ApAuthorFeign;
import com.usian.user.feign.WmUserFeign;
import com.usian.user.mapper.ApUserMapper;
import com.usian.user.mapper.ApUserRealNameMapper;
import com.usian.user.service.ApUserRealNameService;
import io.seata.spring.annotation.GlobalTransactional;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ApUserRealNameServiceImpl extends ServiceImpl<ApUserRealNameMapper, ApUserRealname>
        implements ApUserRealNameService {


    @Autowired
    private ApUserMapper apUserMapper;
    @Autowired
    private WmUserFeign wmUserFeign;
    @Autowired
    private ApAuthorFeign apAuthorFeign;

    @Override
    public ResponseResult findListByStatus(AuthDto dto) {

        //1:校验参数
        if(dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        dto.checkParam();

        //2:封装查询条件
        QueryWrapper<ApUserRealname> queryWrapper =new QueryWrapper<>();
        if(dto.getStatus()!=null) {
            queryWrapper.lambda().eq(ApUserRealname::getStatus, dto.getStatus());
        }
        queryWrapper.lambda().orderByDesc(ApUserRealname::getCreatedTime);
        Page page =new Page(dto.getPage(),dto.getSize());
        IPage iPage = this.page(page, queryWrapper);

        //3:结果集封装到 ResponseResult
        ResponseResult responseResult =new PageResponseResult(dto.getPage(),dto.getSize(),(int)iPage.getTotal());
        responseResult.setData(iPage.getRecords());
        return responseResult;
    }

    @GlobalTransactional
    @Override
    public ResponseResult updateStatusById(AuthDto dto) {

        /**
         * 1:校验参数
         * 2：update  ap_user_realname   status  reason
         * 3: 判断审核状态  9成功 2 失败  wmuser  apauthor  查询   再保存
         * 4：更新apuser flag  1 自媒体人
         */

        if(dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        if(dto.getStatus()!=9 && dto.getStatus()!=2){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //成功
        ApUserRealname apUserRealname = this.getById(dto.getId());
        apUserRealname.setStatus(dto.getStatus());
        apUserRealname.setUpdatedTime(new Date());
        if(StringUtils.isNotBlank(dto.getMsg())){
            apUserRealname.setReason(dto.getMsg());
        }
        this.updateById(apUserRealname);

        if(dto.getStatus()==9){
            //录入wmuser、apauthor表
            ResponseResult result = this.createWmUserAndAuthor(dto);
            if(result.getCode() !=0){
                return result;
            }
        }

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    private ResponseResult createWmUserAndAuthor(AuthDto dto){

        ApUserRealname apUserRealname = this.getById(dto.getId());
        if(apUserRealname ==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        ApUser apUser = apUserMapper.selectById(apUserRealname.getUserId());
        if(apUser==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //查询账户是否存在，如存在则不创建  反之
        int result =0;
        WmUser wmUser = wmUserFeign.findByName(apUser.getName());
        if(wmUser ==null){
            wmUser =new WmUser();
            wmUser.setApUserId(apUser.getId());
            wmUser.setCreatedTime(new Date());
            wmUser.setSalt(apUser.getSalt());
            wmUser.setName(apUser.getName());
            wmUser.setPassword(apUser.getPassword());
            wmUser.setStatus(9); //9：默认可用账号
            wmUser.setPhone(apUser.getPhone());
            wmUserFeign.saveWmUser(wmUser);
            result++;
        }
        //apauthor
        ApAuthor apAuthor = apAuthorFeign.findByUserId(apUser.getId());
        if(apAuthor==null){
            apAuthor =new ApAuthor();
            apAuthor.setName(wmUser.getName());
            apAuthor.setType(2); //2 平台自媒体人
            apAuthor.setCreatedTime(new Date());
            apAuthor.setUserId(apUser.getId());

            WmUser wmUser1 = wmUserFeign.findByName(wmUser.getName());
            apAuthor.setWmUserId(wmUser1.getId());
            apAuthorFeign.saveAuthor(apAuthor);
            result++;
        }

        if(result>0){
            //跟新apuser flag
            apUser.setFlag((short)1); //1:自媒体人
            apUserMapper.updateById(apUser);
            int i=10/0;

            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }else{
            return ResponseResult.okResult(AppHttpCodeEnum.DATA_EXIST);
        }
    }
}
